<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>测试图片打点</title>
  <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <style>
    .img {
      position: relative;
      border: solid 1px #000;
      display: inline-block;
    }

    .img .marker {
      position: absolute;
      width: 20px;
      height: 20px;
      background: #f00;
    }
  </style>
</head>
<body>
<div>
  <p>打点</p>
  <div class="img" id="dv">
    <img id="bigImg" src="../img/a.jpg" width="800px" height="600px">
  </div>
</div>
</body>
</html>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
  var ProportionHeightInImg; //鼠标所选位置相对于所选图片高度的比例
  var ProportionWidthInImg;//鼠标所选位置相对于所选图片宽度的比例
  function createMarker(x, y, divName) {
    var div = document.createElement('div');
    div.className = 'marker'; div.style.left = x + 'px'; div.style.top = y + 'px';
    document.getElementById(divName).appendChild(div)
    console.log("left: " + x + 'px, top: ' + y + 'px')
  }
  document.getElementById('dv').onclick = function (e) {
    e = e || window.event;
    var x = e.offsetX || e.layerX, y = e.offsetY || e.layerY;
    console.log("x: " + x + "y: " + y);
    createMarker(x, y, 'dv');

    //获取图片的高度和宽度
    var myImg = document.querySelector("#bigImg");
    var currWidth = myImg.clientWidth;
    var currHeight = myImg.clientHeight;
    ProportionWidthInImg = x / currWidth;
    ProportionHeightInImg = y / currHeight;
  }
</script>
